package baseFx.tests.common.mq.mysql;

import baseFx.common.Var;
import baseFx.common.concurrent.Task;
import baseFx.common.mq.mysql.MysqlPublisher;
import baseFx.common.mq.mysql.MysqlSubscriber;
import baseFx.common.utils.FxDate;
import baseFx.database.Sql;
import baseFx.database.jdbc.impl.MysqlSql;
import com.mysql.cj.jdbc.MysqlDataSource;

import java.util.Scanner;

public class Run {
    private final static Object lock = new Object();
    private static Var<Integer> log = new Var<>(0);

    public static void main(String[] args) {
        MysqlDataSource ds = new MysqlDataSource();
        ds.setServerName("192.168.1.55");
        ds.setDatabaseName("zhongqitao");
        ds.setUser("dev");
        ds.setPassword("123456");

        Sql pubSql = new MysqlSql(ds);
        MysqlPublisher publisher = new MysqlPublisher(pubSql);
        publisher.init();
        Var<Integer> count = new Var<>(0);
        Var<Integer> sum = new Var<>(0);
        Var<Long> time = new Var<>(new FxDate().addSecond(1).getTime());

        MysqlSubscriber subscriber1 = new MysqlSubscriber(ds);

        MysqlSubscriber subscriber2 = new MysqlSubscriber(ds);

        MysqlSubscriber subscriber3 = new MysqlSubscriber(ds);

        MysqlSubscriber subscriber4 = new MysqlSubscriber(ds);

        subscriber1.eventReceived().add((s, e) -> {
            Task.delay(500);
            synchronized (lock) {
                count.set(count.get() + 1);
                log.set(log.get() + 1);
            }
        });
        subscriber2.eventReceived().add((s, e) -> {
            Task.delay(500);
            synchronized (lock) {
                count.set(count.get() + 1);
                log.set(log.get() + 1);
            }
        });
        subscriber3.eventReceived().add((s, e) -> {
            Task.delay(500);
            synchronized (lock) {
                count.set(count.get() + 1);
                log.set(log.get() + 1);
            }
        });
        subscriber4.eventReceived().add((s, e) -> {
            Task.delay(500);
            synchronized (lock) {
                count.set(count.get() + 1);
                log.set(log.get() + 1);
            }
        });
        subscriber1.consume("aaa", "bbb");
        subscriber2.consume("aaa", "bbb");
        subscriber3.consume("aaa", "bbb");
        subscriber4.consume("aaa", "bbb");

        Task.run(() -> {
            while (true) {
                System.out.println(log.get());
                synchronized (lock) {
                    log.set(0);
                }
                Task.delay(1000);
            }
        });

        Scanner scanner = new Scanner(System.in);
        while (true) {
            String in = scanner.nextLine();
            if ("0".equals(in)) {
                break;
            }
//            String msg = UUID.randomUUID().toString();
//            publisher.post("aaa", msg);
//            System.out.println(msg);
            System.out.println(count.get());
        }
        subscriber1.close();
        subscriber2.close();
        subscriber3.close();
//        try (Transaction tx = pubSql.transaction()) {
//            for (int i = 0; i < 20000; i++) {
//                publisher.post("aaa", UUID.randomUUID().toString());
//                System.out.println(i);
//            }
//            tx.commit();
//        }
        System.out.println("exit");
        System.exit(0);
    }
}
